<template>
  <!--  报备信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="1"
      formType="show"
      :labelWidth="150"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      formValue: {}
    };
  },
  methods: {
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    init() {
      if (this.value.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.value.row[x.fieldName];
          if (x.fieldName == 'isChangePlan') {
            if (x.initialValue == 10041001) {
              this.initForm.find(item => item.fieldName === 'planDesc').rules[0].required = true;
              this.initForm.find(item => item.fieldName === 'reportUrl').rules[0].required = true;
            }
          }
        });
      }
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '是否变更报备',
          fieldName: 'isChangePlan',
          initialValue: '',
          placeholder: '请选择...',
          itemList: this.createDictList('1004'),
          change: this.isAllChange,
          rules: [{ required: true, message: '请选择是否需要变更报备', trigger: 'change' }]
        },
        {
          type: 'TEXT_AREA',
          label: '报备描述',
          fieldName: 'planDesc',
          initialValue: '',
          selfCols:2,
          placeholder: '',
          maxlength: 1000,
          rules: [{ required: false, message: '请填加报备描述', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '报备附件',
          fieldName: 'reportUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 5
          },
          rules: [{ required: false, message: '请上传报备附件', trigger: 'blur' }]
        }
      ];
    },
    //是否需要变更报备
    isAllChange(val) {
      if (val === 10041001) {
        this.initForm.find(item => item.fieldName === 'planDesc').rules[0].required = true;
        this.initForm.find(item => item.fieldName === 'reportUrl').rules[0].required = true;
      } else {
        this.initForm.find(item => item.fieldName === 'planDesc').rules[0].required = false;
        this.initForm.find(item => item.fieldName === 'reportUrl').rules[0].required = false;
        this.$refs.formPanel.$refs.form.clearValidate();
      }
    },
    formChangeHandle(val) {
      this.formValue = val;
      this.$emit('formchange', this.formValue);
    }
  },
  created() {
    this.init();
  }
};
</script>
